package mx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import mx.entity.Notice;
import mx.entity.PageBean;
import mx.service.NoticeService;
import mx.utils.DateJsonValueProcessor;
import mx.utils.DateUtil;
import mx.utils.ResponseUtil;
import mx.utils.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 通知controller
 * 
 * @author wkk
 *
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {
	@Resource
	private NoticeService noticeService;

	@RequestMapping("/list")
	public String list(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows, Notice notice, HttpServletResponse response)
			throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", StringUtil.formatLike(notice.getName()));
		map.put("title", StringUtil.formatLike(notice.getTitle()));
		map.put("content", StringUtil.formatLike(notice.getContent()));
		map.put("employee", notice.getEmployee());
		map.put("toids", StringUtil.formatLike(notice.getToids()));
		map.put("num", notice.getNum());
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Notice> noticeList = noticeService.list(map);

		long totatl = noticeService.getTotal(map);
		JSONObject result = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
		JSONArray jsonArray = JSONArray.fromObject(noticeList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", totatl);

		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 根据id删除通知信息
	 * 
	 * 
	 */
	@RequestMapping(value = "/delete")
	public String deleteComById(@RequestParam(value = "ids", required = false) String ids, HttpServletResponse response)
			throws Exception {
		String[] id = ids.split(",");

		for (String noticeId : id) {
			noticeService.delete(Integer.parseInt(noticeId));
		}

		JSONObject result = new JSONObject();
		result.put("success", true);
		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 增加通知（选择性填入参数）
	 * 
	 * 
	 */
	@RequestMapping("/save")
	public String save(Notice notice, HttpServletResponse response) throws Exception {
		int resultTotal = 0;
		String num = String.valueOf((new Date().getTime())).substring(5);

		notice.setNum(Integer.parseInt(num));
		JSONObject result = new JSONObject();
		if (notice.getId() == null) {
	
			resultTotal = noticeService.add(notice);
		} else {
			resultTotal = noticeService.update(notice);
		}
		if (resultTotal > 0) {
			result.put("success", true);
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}

}
